$(function() {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/Admin/getDatas",
		paramFunc: paramFunc,
		operation: {
			"edit": ["Admin/Admin/edit/#id"],
			"icon-lock": [initPassword, "初始化密码"],
			"icon-refresh": [changeStatus, "启用/冻结"],
		},
		hasCheckbox: false,
		afterFunc:afterFunc
	});

	// exportEvent();
});

var paramFunc = function() {
	var data = getFormJson();
	return data;
};

var afterFunc = function(){

    $("#table tbody tr").each(function() {
        var data = $("#table").getOpts().datas[$(this).index()];
        if(data.admin_type==1){
        	$(this).children("td:nth-child(2)").find(".icon-lock").hide();
        }
    });
}

var changeStatus = function(idx, trData) {
	var msg = "";
	if(trData.status == 1) {
		msg = "是否确认冻结【" + trData.username + "】?";
	} else {
		msg = "是否确认启用【" + trData.username + "】?";
	}
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Admin/changeStatus/"+trData.id+"/"+trData.status,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
	                    window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

var initPassword = function(idx, trData) {
	var msg = "是否确认初始化【" + trData.username + "】的登录密码?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Admin/initPassword/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

//导出数据
exportEvent = function() {
	$("input").keypress(function(event) {
		if(event.keyCode == "13") {
			$("#table").search();
		}
	})
	$("#export").click(function() {
		var ret = $("#table").getTableThInfo();
		ret.params = usefulArray(paramFunc());
		ret.title = "会员列表";
		locationPost("User/commonExport", {
			'json': JSON.stringify(ret)
		});
	});
};



//用户数据同步
function adminDataSynchronization() {
    var msg = "统一认证用户数据同步，该操作可能需要较长时间，操作中切勿关闭网页，是否继续?";
    swal({
        title: "请确认",
        text: msg,
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        confirmButtonText: "确定",
        cancelButtonText: '取消',
        confirmButtonColor: "#ec6c62"
    }, function(isConfirm) {
        if (isConfirm) {
        	d = dialog({
        		title: '',
        		content: "统一认证中...",
        	});
        	d.showModal();
        	$.post("CommonApi/CommonZjipcCa/getToken", {},
        	    function(ret, err) {
        	    	if(d != undefined){
				    	d.close().remove();
				    }
        	        if (ret.status) {
        	            getUserList(ret.v);
        	        } else {
        	            window.parent.topMessagePrompt('error',ret.msg);
        	        }
        	    },
        	    "json"); //这里返回的类型有：json,html,xml,text

           
        }
    });
}


function getUserList(token){
	d = dialog({
		title: '',
		content: "正在获取统一认证最新用户数据...",
	});
	d.showModal();
	$.ajax({
	    type: 'POST',
	    url: $('#zjipc_auth_url').val()+'Api/User',
	    data: {
	        appid: $('#zjipc_app_id').val(),
	        method: 'userList',
	        token: token,
	    },
	    async: false,
	    dataType: 'jsonp',
	    success: function(ret) {
	    	if(d != undefined){
				d.close().remove();
			}
	    	if(ret.status){
	    		var t=JSON.parse(ret.v);
	    		doAdminDataSynchronization(t);
	    	}
	    	else{
        	    window.parent.topMessagePrompt('error',ret.msg);
	    	}
	    }
	});
}


//ajax访问数据同步的操作函数，传递的对象转化为json字符串，防止超出php post最大限制
function doAdminDataSynchronization(userlist){
	d = dialog({
		title: '',
		content: "服务器数据更新中...",
	});
	d.showModal();
	$.ajax({
	    type: 'POST',
	    url: "Admin/Admin/doAdminDataSynchronization",
	    data: {
	        admin_list:JSON.stringify(userlist)
	    },
	    async: false,
	    dataType: 'json',
	    success: function(ret) {
	    	if(d != undefined){
				d.close().remove();
			}
	    	if(ret.status){
	    		window.parent.topMessagePrompt('success',ret.msg);
	    		window.parent.refreshTable();
	    	}
	    	else{
	    		
        	    window.parent.topMessagePrompt('error',ret.msg);
	    	}
	    }
	});
}